package com.helloae.chap09;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.PolygonElement;
import com.esri.arcgis.carto.esriSelectionResultEnum;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureCursorProxy;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.ISelectionSet;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.geodatabase.SpatialFilter;
import com.esri.arcgis.geodatabase.esriSpatialRelEnum;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * <B>代码功能：</B>查询爱达荷州州际公路8km以内，温度超过60°的温泉<br>
 * <B>思路：</B>先通过属性查询道路图层，然后对返回结果缓冲，之后再使用缓冲为空间查询条件，temp>60为属性查询条件<br>
 * <B>注意：</B>缓冲的主要接口是ITopologicalOperator，但是点、线、多边形等要素均实现了该接口，因此本程序中只能看到Polyline类，而不是ITopologicalOperator<br>
 * <B>示例章节：</B>9.5.1
 * @author zhushijie
 *
 */
public class BufferSelect {
	public static void main(String[] args) throws UnknownHostException,
			IOException {
		//初始化AE许可
		EngineInitializer.initializeVisualBeans();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		//导入数据
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap09", 0);

		IFeatureClass pRoadClass = pFeatureWorkspace
				.openFeatureClass("idroads.shp");
		IFeatureClass pThermalClass = pFeatureWorkspace
				.openFeatureClass("thermal.shp");
		FeatureLayer pRoadLayer = new FeatureLayer();
		pRoadLayer.setFeatureClassByRef(pRoadClass);
		FeatureLayer pThermalLayer = new FeatureLayer();
		pThermalLayer.setFeatureClassByRef(pThermalClass);
		
		//查询Route_Desc = 'Interstate'的道路
		QueryFilter pQueryFilter = new QueryFilter();
		pQueryFilter.setWhereClause("Route_Desc = 'Interstate'");
		pRoadLayer.selectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
		pRoadLayer.setBufferDistance(8000);
		ISelectionSet pRoadSelSet = pRoadLayer.getSelectionSet();
		ICursor[] pCursor = new ICursor[1];
		pRoadSelSet.search(null, false, pCursor);
		
		//设置查找条件：温度大于60
		SpatialFilter pSpatialFilter = new SpatialFilter();
		pSpatialFilter.setWhereClause("temp > 60");
		pSpatialFilter.setSpatialRel(esriSpatialRelEnum.esriSpatialRelContains);
		
		IFeatureCursor pRoadCursor = new IFeatureCursorProxy(pCursor[0]);
		IFeature pRoad = pRoadCursor.nextFeature();
		//遍历所有符合条件的道路，缓冲并查询温度大于60的要素
		while(pRoad != null){
			Polyline pTopoOperator = (Polyline) pRoad.getShape();
			PolygonElement pElement = new PolygonElement();
			pElement.setGeometry(pTopoOperator.buffer(8000));
			pSpatialFilter.setGeometryByRef(pElement.getGeometry());
			pThermalLayer.selectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
			pRoad = pRoadCursor.nextFeature();
		}
		System.out.println(pThermalLayer.getSelectionSet().getCount());
		
		//上面代码以完成全部查询，以下代码仅仅是将查询结果添加到可视化界面中
        MapBean map = new MapBean();
        //添加图层
        map.addLayer(pThermalLayer, 0);
        map.addLayer(pRoadLayer, 1);
        
        JFrame frame = new JFrame("Hello Arcobjects");
        frame.add(map, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
	}

}
